Imports Microsoft.VisualBasic
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Imports System.Data
Imports DataAccesGenerales
Imports DataAcces

Public Class DataAccesBuques
  Private Const consTimeOut As Integer = 0 
  
  ''' <summary>
  ''' Funcion para Insertar un regristro en la tabla Buques
  ''' </summary>
  Public Function Buques_Insertar(pnom_Corto_Buque As String, pdesc_Buque As String, ppais As Integer, ptipo_Buq As Integer, pcalado As Integer, peslora As Integer, pganchos As Integer, pTRB As Integer, pmanga As Integer, ppuntal As Integer, pexcento_Puerto As String, pexcento_Atraque As String, pnum_Serie As String, pfecha_Modifica As Date, phora_Modifica As String, pnumeral As String, pTRN As Integer, pcBaja As Char, punidad_Calado As String, pcombus_IFO As Long, pcombus_DO As Long, punidad_Combus As String, pcalado_Popa As Integer, pMMSI As String, plinea_Naviera As Integer) As Byte
    Dim pTransaccion As Byte = 0
    Dim mensaje As String = String.Empty
    Try
      Using oraConnection As New Oracle.DataAccess.Client.OracleConnection(ConfigurationManager.ConnectionStrings("EDIV2DESOraClient").ConnectionString)
        Using oraCommandDocumentos As New Oracle.DataAccess.Client.OracleCommand()
          oraCommandDocumentos.Connection = oraConnection
          oraCommandDocumentos.CommandTimeout = consTimeOut
          oraCommandDocumentos.CommandType = CommandType.StoredProcedure
          oraCommandDocumentos.CommandText = "EDIV2.MI_EDIV2GENERALES.BuquesInsertar" 
          oraCommandDocumentos.Parameters.Add("pnom_Corto_Buque", pnom_Corto_Buque)
          oraCommandDocumentos.Parameters.Add("pdesc_Buque", pdesc_Buque)
          oraCommandDocumentos.Parameters.Add("ppais", ppais)
          oraCommandDocumentos.Parameters.Add("ptipo_Buq", ptipo_Buq)
          oraCommandDocumentos.Parameters.Add("pcalado", pcalado)
          oraCommandDocumentos.Parameters.Add("peslora", peslora)
          oraCommandDocumentos.Parameters.Add("pganchos", pganchos)
          oraCommandDocumentos.Parameters.Add("pTRB", pTRB)
          oraCommandDocumentos.Parameters.Add("pmanga", pmanga)
          oraCommandDocumentos.Parameters.Add("ppuntal", ppuntal)
          oraCommandDocumentos.Parameters.Add("pexcento_Puerto", pexcento_Puerto)
          oraCommandDocumentos.Parameters.Add("pexcento_Atraque", pexcento_Atraque)
          oraCommandDocumentos.Parameters.Add("pnum_Serie", pnum_Serie)
          oraCommandDocumentos.Parameters.Add("pfecha_Modifica", pfecha_Modifica)
          oraCommandDocumentos.Parameters.Add("phora_Modifica", phora_Modifica)
          oraCommandDocumentos.Parameters.Add("pnumeral", pnumeral)
          oraCommandDocumentos.Parameters.Add("pTRN", pTRN)
          oraCommandDocumentos.Parameters.Add("pcBaja", pcBaja)
          oraCommandDocumentos.Parameters.Add("punidad_Calado", punidad_Calado)
          oraCommandDocumentos.Parameters.Add("pcombus_IFO", pcombus_IFO)
          oraCommandDocumentos.Parameters.Add("pcombus_DO", pcombus_DO)
          oraCommandDocumentos.Parameters.Add("punidad_Combus", punidad_Combus)
          oraCommandDocumentos.Parameters.Add("pcalado_Popa", pcalado_Popa)
          oraCommandDocumentos.Parameters.Add("pMMSI", pMMSI)
          oraCommandDocumentos.Parameters.Add("plinea_Naviera", plinea_Naviera)

          oraCommandDocumentos.Parameters.Add("pTransaccion", Oracle.DataAccess.Client.OracleDbType.Byte).Direction = ParameterDirection.Output
          oraCommandDocumentos.Parameters.Add("pMensaje", Oracle.DataAccess.Client.OracleDbType.Varchar2, 200).Direction = ParameterDirection.Output

          oraConnection.Open()
          oraCommandDocumentos.ExecuteNonQuery()
          pTransaccion = CByte(oraCommandDocumentos.Parameters("pTransaccion").Value.ToString())
          mensaje = oraCommandDocumentos.Parameters("pMensaje").Value.ToString()
          oraConnection.Close()
        End Using
      End Using
    Catch ex As Exception
      Throw ex
    End Try
    Return pTransaccion
  End Function

  ''' <summary>
  ''' Funcion para Modificar un regristro en la tabla Buques
  ''' </summary>
  Public Function Buques_Modificar(pnom_Corto_Buque As String, pdesc_Buque As String, ppais As Integer, ptipo_Buq As Integer, pcalado As Integer, peslora As Integer, pganchos As Integer, pTRB As Integer, pmanga As Integer, ppuntal As Integer, pexcento_Puerto As String, pexcento_Atraque As String, pnum_Serie As String, pfecha_Modifica As Date, phora_Modifica As String, pnumeral As String, pTRN As Integer, pcBaja As Char, punidad_Calado As String, pcombus_IFO As Long, pcombus_DO As Long, punidad_Combus As String, pcalado_Popa As Integer, pMMSI As String, plinea_Naviera As Integer) As Byte
    Dim pTransaccion As Byte = 0
    Dim mensaje As String = String.Empty
    Try
      Using oraConnection As New Oracle.DataAccess.Client.OracleConnection(ConfigurationManager.ConnectionStrings("EDIV2DESOraClient").ConnectionString)
        Using oraCommandDocumentos As New Oracle.DataAccess.Client.OracleCommand()
          oraCommandDocumentos.Connection = oraConnection
          oraCommandDocumentos.CommandTimeout = consTimeOut
          oraCommandDocumentos.CommandType = CommandType.StoredProcedure
          oraCommandDocumentos.CommandText = "EDIV2.MI_EDIV2GENERALES.BuquesModificar"
          oraCommandDocumentos.Parameters.Add("pnom_Corto_Buque", pnom_Corto_Buque)
          oraCommandDocumentos.Parameters.Add("pdesc_Buque", pdesc_Buque)
          oraCommandDocumentos.Parameters.Add("ppais", ppais)
          oraCommandDocumentos.Parameters.Add("ptipo_Buq", ptipo_Buq)
          oraCommandDocumentos.Parameters.Add("pcalado", pcalado)
          oraCommandDocumentos.Parameters.Add("peslora", peslora)
          oraCommandDocumentos.Parameters.Add("pganchos", pganchos)
          oraCommandDocumentos.Parameters.Add("pTRB", pTRB)
          oraCommandDocumentos.Parameters.Add("pmanga", pmanga)
          oraCommandDocumentos.Parameters.Add("ppuntal", ppuntal)
          oraCommandDocumentos.Parameters.Add("pexcento_Puerto", pexcento_Puerto)
          oraCommandDocumentos.Parameters.Add("pexcento_Atraque", pexcento_Atraque)
          oraCommandDocumentos.Parameters.Add("pnum_Serie", pnum_Serie)
          oraCommandDocumentos.Parameters.Add("pfecha_Modifica", pfecha_Modifica)
          oraCommandDocumentos.Parameters.Add("phora_Modifica", phora_Modifica)
          oraCommandDocumentos.Parameters.Add("pnumeral", pnumeral)
          oraCommandDocumentos.Parameters.Add("pTRN", pTRN)
          oraCommandDocumentos.Parameters.Add("pcBaja", pcBaja)
          oraCommandDocumentos.Parameters.Add("punidad_Calado", punidad_Calado)
          oraCommandDocumentos.Parameters.Add("pcombus_IFO", pcombus_IFO)
          oraCommandDocumentos.Parameters.Add("pcombus_DO", pcombus_DO)
          oraCommandDocumentos.Parameters.Add("punidad_Combus", punidad_Combus)
          oraCommandDocumentos.Parameters.Add("pcalado_Popa", pcalado_Popa)
          oraCommandDocumentos.Parameters.Add("pMMSI", pMMSI)
          oraCommandDocumentos.Parameters.Add("plinea_Naviera", plinea_Naviera)

          oraCommandDocumentos.Parameters.Add("pTransaccion", Oracle.DataAccess.Client.OracleDbType.Byte).Direction = ParameterDirection.Output
          oraCommandDocumentos.Parameters.Add("pMensaje", Oracle.DataAccess.Client.OracleDbType.Varchar2, 200).Direction = ParameterDirection.Output

          oraConnection.Open()
          oraCommandDocumentos.ExecuteNonQuery()
          pTransaccion = CByte(oraCommandDocumentos.Parameters("pTransaccion").Value.ToString())
          mensaje = oraCommandDocumentos.Parameters("pMensaje").Value.ToString()
          oraConnection.Close()
        End Using
      End Using
    Catch ex As Exception
      Throw ex
    End Try
    Return pTransaccion
  End Function
  
  ''' <summary>
  ''' Funcion para consultar un registro de la tabla Buques
  ''' </summary>
  ''' <param name="nidDocumento">numero de documento</param>
  ''' <returns>DataTable</returns>
  ''' <remarks></remarks>
  Public Function Buques_Consultar(pNidDocumento as Integer) As DataTable
    Dim dtResultado As New DataTable("BuquesConsulta")
    Try
      Using oraConnection As New Oracle.DataAccess.Client.OracleConnection(ConfigurationManager.ConnectionStrings("EDIV2DESOraClient").ConnectionString)
        Using oraCommandDocumentos As New Oracle.DataAccess.Client.OracleCommand()
          oraCommandDocumentos.Connection = oraConnection
          oraCommandDocumentos.CommandTimeout = consTimeOut
          oraCommandDocumentos.CommandType = CommandType.StoredProcedure
          oraCommandDocumentos.CommandText = "EDIV2.MI_EDIV2GENERALES.BuquesConsultar" 
          oraCommandDocumentos.Parameters.Add("pNidDocumento", pNidDocumento)
          oraCommandDocumentos.Parameters.Add("pTransaccion", Oracle.DataAccess.Client.OracleDbType.Byte).Direction = ParameterDirection.Output
          oraCommandDocumentos.Parameters.Add(New Oracle.DataAccess.Client.OracleParameter("pCursor", OracleDbType.RefCursor)).Direction = ParameterDirection.Output

          Dim oraDataAdapter As New OracleDataAdapter(oraCommandDocumentos)
          oraDataAdapter.Fill(dtResultado)
          oraConnection.Close()
        End Using
      End Using
    Catch ex As Exception
      Throw ex
    End Try
    Return dtResultado
  End Function
  
  ''' <summary>
  ''' Funcion para generar un combo de la tabla Buques
  ''' </summary>
  ''' <param name="pNombre">nombre a buscar en la tabla</param>
  ''' <returns>DataTable</returns>
  ''' <remarks>si el nombre es "" retornara todos los registros</remarks>
  Public Function Buques_Combo(pNombre As String) As DataTable
    Dim dtResultado As New DataTable("BuquesCombo")
    Try
      Using oraConnection As New Oracle.DataAccess.Client.OracleConnection(ConfigurationManager.ConnectionStrings("EDIV2DESOraClient").ConnectionString)
        Using oraCommandDocumentos As New Oracle.DataAccess.Client.OracleCommand()
          oraCommandDocumentos.Connection = oraConnection
          oraCommandDocumentos.CommandTimeout = consTimeOut
          oraCommandDocumentos.CommandType = CommandType.StoredProcedure
          oraCommandDocumentos.CommandText = "EDIV2.MI_EDIV2GENERALES.BuquesCombo" 
		  oraCommandDocumentos.Parameters.Add("pNombre", pNombre)
          oraCommandDocumentos.Parameters.Add("pTransaccion", Oracle.DataAccess.Client.OracleDbType.Byte).Direction = ParameterDirection.Output
          oraCommandDocumentos.Parameters.Add(New Oracle.DataAccess.Client.OracleParameter("pCursor", OracleDbType.RefCursor)).Direction = ParameterDirection.Output

          Dim oraDataAdapter As New OracleDataAdapter(oraCommandDocumentos)
          oraDataAdapter.Fill(dtResultado)
          oraConnection.Close()
        End Using
      End Using
    Catch ex As Exception
      Throw ex
    End Try
    Return dtResultado
  End Function
  
  ''' <summary>
  ''' Funcion para generar la cadena XML Chilkat del documento de la tabla Buques
  ''' </summary>
  ''' <param name="nidDocumento"> numero del documento</param>
  ''' <returns>Chilkat.Xml</returns>
  ''' <remarks></remarks>
  Public Function Buques_CadenaOriginalXMLChilkat(nidDocumento As Integer) As Chilkat.Xml
    Dim detalleDataSet As New DataSet("DatosBuques")
    Try
      detalleDataSet = Buques_DetalleXML(nidDocumento)
	  'En esta parte van las tablas obtenidas en la consulta anterior
	  'separadas dependiendo del reporte a crear, ejemplo:
	  detalleDataSet.Tables(0).TableName = "Informacion del Tramite"
      detalleDataSet.Tables(1).TableName = "Datos Buques"
      detalleDataSet.Tables(2).TableName = "Archivos Anexos"
      detalleDataSet.Tables(3).TableName = "Incidencias"

      Dim xml As New Chilkat.Xml
      xml.LoadXml(detalleDataSet.GetXml())
      Return xml 'detalleDataSet.GetXml()
    Catch ex As Exception
      Throw ex
    End Try
  End Function

  ''' <summary>
  ''' Funcion para consultar el detalle del documento de la tabla Buques
  ''' </summary>
  ''' <param name="nidDocumento">numero del documento</param>
  ''' <returns>DataSet</returns>
  ''' <remarks></remarks>
  Public Function Buques_DetalleXML(ByVal nidDocumento As Integer) As DataSet
    Dim dsDetalle As New DataSet("BuquesDetalle")
    Dim pTransaccion As Byte = 0
    Try
      Using oraConnection As New Oracle.DataAccess.Client.OracleConnection(ConfigurationManager.ConnectionStrings("EDIV2DESOraClient").ConnectionString)
        Using oraCommandDocumentos As New Oracle.DataAccess.Client.OracleCommand()
          oraCommandDocumentos.Connection = oraConnection
          oraCommandDocumentos.CommandTimeout = consTimeOut
          oraCommandDocumentos.CommandType = CommandType.StoredProcedure
          oraCommandDocumentos.CommandText = "EDIV2.MI_EDIV2GENERALES.BuquesDetalleXML"
          oraCommandDocumentos.Parameters.Add("pNidDocumento", nidDocumento)
          oraCommandDocumentos.Parameters.Add("pTransaccion", Oracle.DataAccess.Client.OracleDbType.Byte).Direction = ParameterDirection.Output
          'En esta parte es para los cursores necesario 
		  oraCommandDocumentos.Parameters.Add(New Oracle.DataAccess.Client.OracleParameter("pEncabezado", OracleDbType.RefCursor)).Direction = ParameterDirection.Output
          oraCommandDocumentos.Parameters.Add(New Oracle.DataAccess.Client.OracleParameter("pDatos", OracleDbType.RefCursor)).Direction = ParameterDirection.Output
          oraCommandDocumentos.Parameters.Add(New Oracle.DataAccess.Client.OracleParameter("pAnexos", OracleDbType.RefCursor)).Direction = ParameterDirection.Output
          oraCommandDocumentos.Parameters.Add(New Oracle.DataAccess.Client.OracleParameter("pIncidencias", OracleDbType.RefCursor)).Direction = ParameterDirection.Output
		
          Dim oraDataAdapter As New OracleDataAdapter(oraCommandDocumentos)
          'pTransaccion = CByte(oraCommandDocumentos.Parameters("pTransaccion").Value.ToString())
          oraDataAdapter.Fill(dsDetalle)
        End Using
      End Using
    Catch ex As Exception
      Throw ex
    End Try
    Return dsDetalle
  End Function
  
  ''' <summary>
  ''' Funcion para generar la integracion del documento de la tabla Buques
  ''' </summary>
  ''' <param name="pNidDocumento">numero del documento</param>
  ''' <returns>Byte</returns>
  ''' <remarks></remarks>
  Public Function Buques_Integracion(pNidDocumento As Integer) As Byte
    Dim pTransaccion As Byte = 0
    Dim mensaje As String = String.Empty
    Try
      Using oraConnection As New Oracle.DataAccess.Client.OracleConnection(ConfigurationManager.ConnectionStrings("EDIV2DESOraClient").ConnectionString)
        Using oraCommandDocumentos As New Oracle.DataAccess.Client.OracleCommand()
          oraCommandDocumentos.Connection = oraConnection
          oraCommandDocumentos.CommandTimeout = consTimeOut
          oraCommandDocumentos.CommandType = CommandType.StoredProcedure
          oraCommandDocumentos.CommandText = "EDIV2.MI_EDIV2GENERALES.BuquesIntegracion"
          oraCommandDocumentos.Parameters.Add("pNidDocumento", pNidDocumento)
          oraCommandDocumentos.Parameters.Add("pTransaccion", Oracle.DataAccess.Client.OracleDbType.Byte).Direction = ParameterDirection.Output
          oraCommandDocumentos.Parameters.Add("pMensaje", Oracle.DataAccess.Client.OracleDbType.Varchar2, 200).Direction = ParameterDirection.Output

          oraConnection.Open()
          oraCommandDocumentos.ExecuteNonQuery()
          pTransaccion = CByte(oraCommandDocumentos.Parameters("pTransaccion").Value.ToString())
          mensaje = oraCommandDocumentos.Parameters("pMensaje").Value.ToString()
          oraConnection.Close()
        End Using
      End Using
    Catch ex As Exception
      Throw ex
    End Try
    Return pTransaccion
  End Function
  
  Public Function Buques_Borrar(pnid As Integer) As Byte
    Dim pTransaccion As Byte = 0
    Dim mensaje As String = String.Empty
    Try
      Using oraConnection As New Oracle.DataAccess.Client.OracleConnection(ConfigurationManager.ConnectionStrings("EDIV2DESOraClient").ConnectionString)
        Using oraCommandDocumentos As New Oracle.DataAccess.Client.OracleCommand()
          oraCommandDocumentos.Connection = oraConnection
          oraCommandDocumentos.CommandTimeout = consTimeOut
          oraCommandDocumentos.CommandType = CommandType.StoredProcedure
          oraCommandDocumentos.CommandText = "EDIV2.MI_EDIV2GENERALES.BuquesBorrar"
          oraCommandDocumentos.Parameters.Add("pnid", pnid)
          oraCommandDocumentos.Parameters.Add("pTransaccion", Oracle.DataAccess.Client.OracleDbType.Byte).Direction = ParameterDirection.Output
          
          oraConnection.Open()
          oraCommandDocumentos.ExecuteNonQuery()
          pTransaccion = CByte(oraCommandDocumentos.Parameters("pTransaccion").Value.ToString())
          oraConnection.Close()
        End Using
      End Using
    Catch ex As Exception
      Throw ex
    End Try
    Return pTransaccion
  End Function
  
End Class
